package com.zhongge.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.zhongge.entity.Region;
import com.zhongge.service.impl.RegionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhongge
 * @since 2025-11-09
 */
@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private RegionServiceImpl regionService;

    @GetMapping("/initRegion")
    @ResponseBody
    public JsonNode initRegion(){
        try {
            return regionService.initRegion();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //获取省份
    @PostMapping("/provinces")
    @ResponseBody
    public List<Region> getProvinces(){
        return regionService.getProvinces();
    }

    //根据省份id获取其下的所有市区
    @PostMapping("/cities")
    @ResponseBody
    public List<Region> getCity(Integer provinceId){
        return regionService.getCity(provinceId);
    }

    //根据市id获取其下的所有县
    @PostMapping("/counties")
    @ResponseBody
    public List<Region> getCounties(Integer cityId){
        return regionService.getCounties(cityId);
    }
}
